package com.neusoft.actor.controller;

import com.neusoft.actor.dto.BedUsageDetailDto;
import com.neusoft.actor.po.BedUsage;
import com.neusoft.actor.po.Client;
import com.neusoft.actor.po.ResponseBean;
import com.neusoft.actor.service.BedUsageService;
import com.neusoft.actor.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
//@CrossOrigin("*")
@RequestMapping("/BedUsagesController")
public class BedUsageController {

    @Autowired
    private BedUsageService bedUsageService;
    @Autowired
    private ClientService clientService;

    @GetMapping
    public ResponseBean<List<BedUsage>> getAllBedUsages() {
        return bedUsageService.getAllBedUsages();
    }

    @GetMapping("/queryByCondition")
//    多条件查询
    public ResponseBean<List<BedUsage>> getBedUsageByCondition(@RequestParam String clientName, @RequestParam LocalDate startDate, @RequestParam boolean isCurrent) {
        Client client = clientService.getClientByName(clientName).getData();
        if (client != null) {
            return bedUsageService.getBedUsageByCondition(client.getClientId(), startDate, isCurrent);
        }
        return null;
    }

    @GetMapping("/getBedUsageDetails")
//     多条件查询，连接床位表，客户表
    public ResponseBean<List<BedUsageDetailDto>> getBedUsageDetails(
            @RequestParam(required = false) String clientName,
            @RequestParam(required = false) String admissionDate,
            @RequestParam(required = false) Boolean isCurrent) {
        return bedUsageService.getBedUsageDetails(clientName, admissionDate, isCurrent);
    }

    @PostMapping
    public ResponseBean<Integer> addBedUsage(@RequestBody BedUsage bedUsage) {
        return bedUsageService.addBedUsage(bedUsage);
    }

    @PutMapping("/updateBedUsage")
//  只能修改床位的结束时间
    public ResponseBean<Integer> updateBedUsage(@RequestBody BedUsage bedUsage) {
        return bedUsageService.updateBedUsage(bedUsage);
    }

    @DeleteMapping("/deleteBedUsage")
//    逻辑删除旧床位使用信息（更改为失效）
    public ResponseBean<Integer> deleteBedUsage(@RequestParam Integer id) {
        BedUsage bedUsage = bedUsageService.getBedUsageById(id).getData();
        if (bedUsage != null) {
            bedUsage.setIsCurrent(false);
        }
        return bedUsageService.deleteBedUsage(id);
    }
}